var that;
// 最大行数
var max_row_height = 5;
// 行高
var cart_offset = 90;
// 底部栏偏移量
var food_row_height = 49;

Page({
    data: {
        categoryStates: [],
        cartData: {},
        cartObjects: [],
        maskVisual: 'hidden',
        amount: 0,
        seller: {
            logo: { url: '../../../image/汉堡.jpg' },
            name: '华莱士·全鸡汉堡',
            notice: 'Welcome to our restaurant!',
            business_start: '09:00',
            business_end: '21:00',
            express_fee: 5,
            min_amount: 20
        },
        categoryObjects: [
          { id: '1', title: '人气推荐', priority: 1 },
          { id: '2', title: '餐前福利', priority: 2 },
          { id: '3', title: '爆款特卖', priority: 3 },
          { id: '4', title: '精选套餐', priority: 4 },
          { id: '5', title: '新品上市', priority: 5 },
          { id: '6', title: '华氏全鸡', priority: 6 },
          { id: '7', title: '汉堡和卷', priority: 7 },
          { id: '8', title: '美味小食', priority: 8 },
          { id: '9', title: '甜品饮料', priority: 9 },
 
        ],
        foodObjects: []
    },
    onLoad: function () {
        that = this;
        that.loadCategory();
        that.loadFood();
    },
    loadCategory: function () {
        var categoryStates = [];
        for (var i = 0; i < that.data.categoryObjects.length; i++) {
            categoryStates.push(false);
        }
        that.setData({
            categoryStates: categoryStates
        });
    },
    loadFood: function (category) {
        var foodData = {
            '1': [
                { objectId: '101', title: 'Spring Rolls', summary: 'Crispy rolls with vegetables', price: 5, thumb: { url: '../../../image/汉堡.jpg' } },
                { objectId: '102', title: 'Chicken Wings', summary: 'Spicy chicken wings', price: 8, thumb: { url: '../../../image/汉堡.jpg' } }
            ],
            '2': [
                { objectId: '201', title: 'Beef Steak', summary: 'Grilled beef steak', price: 20, thumb: { url: '../../../image/汉堡.jpg' } },
                { objectId: '202', title: 'Grilled Salmon', summary: 'Grilled salmon with sauce', price: 18, thumb: { url: '../../../image/汉堡.jpg' } }
            ],
            '3': [
                { objectId: '301', title: 'Cheesecake', summary: 'Creamy cheesecake', price: 6, thumb: { url: '../../../image/汉堡.jpg' } },
                { objectId: '302', title: 'Brownie', summary: 'Chocolate brownie', price: 4, thumb: { url: '../../../image/汉堡.jpg' } }
            ],

            '4': [
                { objectId: '301', title: '华粉福利餐', summary: 'Creamy cheesecake', price: 18, thumb: { url: '../../../image/taocan1.jpg' } },
                { objectId: '302', title: '香辣鸡腿堡套餐', summary: 'Chocolate brownie', price: 24, thumb: { url: '../../../image/taocan2.jpg' } },
                { objectId: '302', title: '辣翅套餐', summary: 'Chocolate brownie', price: 24, thumb: { url: '../../../image/taocan3.jpg' } }
            ],
            '5': [
                { objectId: '301', title: '清爽单人餐', summary: 'Creamy cheesecake', price: 19.9, thumb: { url: '../../../image/huashi1.jpg' } },
                { objectId: '302', title: '单人六件套', summary: 'Chocolate brownie', price: 27.9, thumb: { url: '../../../image/huashi2.jpg' } },
                { objectId: '302', title: '嗨吃三人餐', summary: 'Chocolate brownie', price: 65.4, thumb: { url: '../../../image/huashi3.jpg' } }
            ],
            '6': [
                { objectId: '301', title: '脆皮全鸡', summary: 'Creamy cheesecake', price: 18.9, thumb: { url: '../../../image/huashi1.jpg' } },
                { objectId: '302', title: '脆脆鸡', summary: 'Chocolate brownie', price: 19.9, thumb: { url: '../../../image/huashi2.jpg' } },
                { objectId: '302', title: '爱吃不吃单人餐', summary: 'Chocolate brownie', price: 24.8, thumb: { url: '../../../image/huashi3.jpg' } }
            ]
            ,
            '7': [
                { objectId: '301', title: '秘制鸡腿堡', summary: '美味多汁', price: 12, thumb: { url: '../../../image/hanbao1.jpg' } },
                { objectId: '302', title: '香辣鸡腿堡', summary: '回头客推荐', price: 12, thumb: { url: '../../../image/hanbao2.jpg' } },
                { objectId: '302', title: '阳光果蔬鸡肉堡', summary: '不辣', price: 12, thumb: { url: '../../../image/hanbao3.jpg' } }
            ]
            ,
            '8': [
                { objectId: '301', title: '玉米杯', summary: '营养又健康', price: 6, thumb: { url: '../../../image/meiwei1.jpg' } },
                { objectId: '302', title: '紫薯豌豆派', summary: '软糯甜心', price: 7, thumb: { url: '../../../image/meiwei2.jpg' } },
                { objectId: '302', title: '鸡米花', summary: '优质面粉', price: 8, thumb: { url: '../../../image/meiwei3.jpg' } }
            ]
            ,
            '9': [
                { objectId: '301', title: '冰镇柠檬汁', summary: '夏季必备', price: 8, thumb: { url: '../../../image/tianpin1.jpg' } },
                { objectId: '302', title: '无糖可乐', summary: '冰爽', price: 6, thumb: { url: '../../../image/tianpin2.jpg' } },
                { objectId: '302', title: '咖啡', summary: '大众推荐', price: 8, thumb: { url: '../../../image/tianpin3.jpg' } }
            ]

        };

        var categoryId = category ? category.id : '1';
        var foodObjects = foodData[categoryId] || [];

        that.setData({
            foodObjects: foodObjects
        });
    },
    switchCategory: function (e) {
        var index = e.currentTarget.dataset.index;
        var categoryId = that.data.categoryObjects[index].id;
        var category = { id: categoryId };

        var categoryStates = that.data.categoryStates.map(function (item, i) {
            return index == i;
        });

        that.setData({
            category: category,
            categoryStates: categoryStates
        });
        that.loadFood(category);
    },
    checkout: function () {
        var cartObjects = [];
        that.data.cartObjects.forEach(function (item) {
            var cart = {
                title: item.food.title,
                price: item.food.price,
                quantity: item.quantity
            };
            cartObjects.push(cart);
        });

        wx.navigateTo({
            url: '../../order/checkout/checkout?quantity=' + that.data.quantity + '&amount=' + that.data.amount + '&express_fee=' + that.data.seller.express_fee + '&carts=' + JSON.stringify(cartObjects)
        });
    },
    add: function (e) {
        var foodId = e.currentTarget.dataset.foodId;
        var cartData = that.data.cartData;
        var foodCount = cartData[foodId] ? cartData[foodId] : 0;
        cartData[foodId] = ++foodCount;
        that.setData({
            cartData: cartData
        });
        that.cartToArray(foodId);
    },
    subtract: function (e) {
        var foodId = e.currentTarget.dataset.foodId;
        var cartData = that.data.cartData;
        var foodCount = cartData[foodId];
        --foodCount;
        if (foodCount == 0) {
            delete cartData[foodId];
        } else {
            cartData[foodId] = foodCount;
        }
        that.setData({
            cartData: cartData
        });
        that.cartToArray(foodId);
    },
    cartToArray: function (foodId) {
        var cartData = that.data.cartData;
        var cartObjects = that.data.cartObjects;
        var foodObjects = that.data.foodObjects;
        var food = foodObjects.find(item => item.objectId == foodId);

        for (var i = 0; i < cartObjects.length; i++) {
            if (cartObjects[i].food.objectId == foodId) {
                if (cartData[foodId] == undefined) {
                    cartObjects.splice(i, 1);
                } else {
                    cartObjects[i].quantity = cartData[foodId];
                }
                that.setData({
                    cartObjects: cartObjects
                });
                that.amount();
                return;
            }
        }

        var cart = {
            food: food,
            quantity: cartData[foodId]
        };
        cartObjects.push(cart);
        that.setData({
            cartObjects: cartObjects
        });
        that.amount();
    },
    cascadeToggle: function () {
        if (that.data.maskVisual == 'show') {
            that.cascadeDismiss();
        } else {
            that.cascadePopup();
        }
    },
    cascadePopup: function () {
        var animation = wx.createAnimation({
            duration: 300,
            timingFunction: 'ease-in-out',
        });
        that.animation = animation;
        var scrollHeight = (that.data.cartObjects.length <= max_row_height ? that.data.cartObjects.length : max_row_height) * food_row_height;
        var cartHeight = scrollHeight + cart_offset;
        animation.translateY(-cartHeight).step();
        that.setData({
            animationData: that.animation.export(),
            maskVisual: 'show',
            scrollHeight: scrollHeight,
            cartHeight: cartHeight
        });
        var animationMask = wx.createAnimation({
            duration: 150,
            timingFunction: 'linear',
        });
        that.animationMask = animationMask;
        animationMask.opacity(0.8).step();
        that.setData({
            animationMask: that.animationMask.export(),
        });
    },
    cascadeDismiss: function () {
        that.animation.translateY(that.data.cartHeight).step();
        that.setData({
            animationData: that.animation.export()
        });
        that.animationMask.opacity(0).step();
        that.setData({
            animationMask: that.animationMask.export(),
        });
        that.setData({
            maskVisual: 'hidden'
        });
    },
    amount: function() {
        var cartObjects = that.data.cartObjects;
        var amount = 0;
        var quantity = 0;
        cartObjects.forEach(function (item) {
            amount += item.quantity * item.food.price;
            quantity += item.quantity;
        });
        that.setData({
            amount: amount,
            quantity: quantity
        });
    }
});
